package com.sample.action;


import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
 
public class UploadAction extends ActionSupport{
    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;

    public List<File> getUpload() {
        return upload;
    }

    public void setUpload(List<File> upload) {
        this.upload = upload;
    }

    public List<String> getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(List<String> uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public List<String> getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(List<String> uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    @Override
    public String execute() throws Exception {
        //文件保存路径
        String path = ServletActionContext.getServletContext().getRealPath("/upload");
        File file = new File(path);
        //不存在则创建
        if(!file.exists()){
            file.mkdir();
        }
        //循环将文件上传到指定路径
        for(int i = 0; i< upload.size(); i++){
            FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
        }
        return SUCCESS;
    }
}